/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.warehouslist.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.entity.AssetCard;
import com.fuyao.cloud.asset.entity.ac.AssetAc;
import com.fuyao.cloud.asset.entity.warehous.Warehous;
import com.fuyao.cloud.asset.entity.warehous.WarehousList;
import com.fuyao.cloud.asset.enums.AssetStatusEnum;
import com.fuyao.cloud.asset.warehouslist.mapper.WarehousListMapper;
import com.fuyao.cloud.asset.warehouslist.service.WarehousListService;
import com.fuyao.cloud.common.security.service.CloudUser;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 入库清单
 *
 * @author liyue
 * @date 2021-12-06 20:19:48
 */
@Service
public class WarehousListServiceImpl extends ServiceImpl<WarehousListMapper, WarehousList> implements WarehousListService {

	@Transactional
	@Override
	public boolean saveOrUpdateWarehousList(Warehous entity, AssetAc assetAc,List<AssetCard> assetCardList) {
		List<WarehousList> warehousListList = Lists.newArrayList();
		CloudUser user = SecurityUtils.getUser();
		for(AssetCard assetCard: assetCardList){
			WarehousList warehousList = new WarehousList();
			warehousList.setId(null);
			warehousList.setWarehousId(entity.getId());
			warehousList.setContractId(assetAc.getContractId());
			warehousList.setContractName(assetAc.getContractName());
			warehousList.setContractNumber(assetAc.getContractNumber());
			warehousList.setSupplierId(assetAc.getSupplierId());
			warehousList.setSupplierName(assetAc.getSupplierName());
			warehousList.setHouseId(assetAc.getHouseId());
			warehousList.setHouseName(assetAc.getHouseName());
			warehousList.setLocationId(assetAc.getLocationId());
			warehousList.setLocationName(assetAc.getLocationName());
			warehousList.setFullLocation(entity.getFullLocation());
			warehousList.setMethod(assetAc.getMethod());
			warehousList.setParentId(assetAc.getId());
			warehousList.setParentCaseid(entity.getCaseid());
			warehousList.setStatus(Integer.parseInt(AssetStatusEnum.FINISH.getCode()));
			warehousList.setBatchnumber(assetAc.getBatchnumber());
			warehousList.setDetailed(assetAc.getDetailed());
			// 资产相关数据
			warehousList.setAssetCardId(assetCard.getId());
			warehousList.setCtiId(assetCard.getCtiId());
			warehousList.setCtiName(assetCard.getCtiName());
			warehousList.setClassified(assetCard.getClassified());
			warehousList.setClassifiedCode(assetCard.getClassifiedCode());
			warehousList.setPhoto(assetCard.getPhoto());
			warehousList.setBarcode(assetCard.getBarcode());
			warehousList.setName(assetCard.getName());
			warehousList.setModel(assetCard.getModel());
			warehousList.setMethod(assetAc.getMethod());
			warehousList.setUnit(assetCard.getUnit());
			warehousList.setPrice(assetCard.getPrice());
			warehousList.setAmount(assetCard.getAmount());
			warehousList.setNumber(assetCard.getNumber());
			warehousList.setRemarks(assetCard.getRemarks());
			warehousList.setQuantity(1L);

			warehousList.setCreateTime(LocalDateTime.now());
			warehousList.setCreateId(user.getId());
			warehousList.setCreateBy(user.getName());
			warehousList.setDeptId(user.getDeptId());
			warehousList.setDeptFullname(assetCard.getDeptFullname());
			warehousListList.add(warehousList);
		}
		return this.saveOrUpdateBatch(warehousListList);
	}

	@Transactional
	@Override
	public void saveOrUpdateWarehousList(Warehous entity) {
		List<WarehousList> warehousLists = entity.getWarehousList();
		this.saveOrUpdateBatch(warehousLists);
		if(CollUtil.isNotEmpty(warehousLists)){
			warehousLists.forEach(ware->{
				ware.setWarehousId(entity.getId());
			});
		}
		this.saveOrUpdateBatch(warehousLists);
	}
}
